package com.zhj.leetcode;

/**
 * 力扣 21 合并两个有序列表
 */
public class Test21 {

    public static void main(String[] args) {
        ListNode l1 = new ListNode(0, new ListNode(1, new ListNode(2, new ListNode(3))));
        ListNode l2 = new ListNode(2, new ListNode(3, new ListNode(4)));
        ListNode l3 = mergeTwoLists(l1, l2);
        ListNode cur = l3;
        while (cur != null) {
            System.out.println(cur.val);
            cur = cur.next;
        }
    }

    public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null) {
            return l2;
        } else if (l2 == null) {
            return l1;
        } else if (l1.val < l2.val) {
            l1.next = mergeTwoLists(l1.next, l2);
            return l1;
        } else {
            l2.next = mergeTwoLists(l1, l2.next);
            return l2;
        }
    }
}
